<?php 
   require_once 'config.php';
   $action = getValue('action','str','POST','');
   $id = getValue('id');
   $sql = "SELECT * FROM products WHERE pro_id =:id";
   $db = new DB();
   $row = $db->row($sql,array('id' => $id));
   if($action == 'add'){
      $id = getValue('pro_id','int','POST');
      $pro_name = getValue('pro_name','str','POST','');
      $pro_intro = getValue('pro_intro','str','POST','');
      $pro_image = getValue('pro_image','str','POST','');
      $pro_summary = getValue('pro_summary','str','POST','');
      $pro_overview = getValue('pro_overview','str','POST','');
      $pro_story = getValue('pro_story','str','POST','');
      $pro_science = getValue('pro_science','str','POST','');
      $pro_benefit = getValue('pro_benefit','str','POST','');
      $pro_video = getValue('pro_video','str','POST','');
      $pro_image_video = getValue('pro_image_video','str','POST','');
      $pro_date = time();
      if(Form::isValid("myForm")){
         if(isset($_FILES['image_field'])){
            $handle = new upload($_FILES['image_field'],'vn_VN');
            if ($handle->uploaded) {
               $handle->allowed = array('image/*');
               $handle->file_max_size = 2*1024*1024;
               $handle->file_new_name_body   = dechex(rand(100,999)).Str::IDEncode(time());
               $handle->image_resize         = true;
               $handle->image_x              = 800;
               $handle->image_ratio_y        = true;
               $handle->process(PATH_IMG_UPLOAD);
               $pro_image = $handle->file_dst_name;
               if ($handle->processed) {
                   $handle->clean();
               } else {
                  $error_upload = Str::alert($handle->error,'alert-error');
               }
           }
         }
         $sql = "UPDATE 
                     products 
                 SET 
                     pro_name =:name, 
                     pro_intro =:intro,
                     pro_summary =:summary, 
                     pro_overview =:overview, 
                     pro_story =:story, 
                     pro_science =:science, 
                     pro_benefit =:benefit, 
                     pro_image =:image,
                     pro_image_video =:image_video,
                     pro_video =:video, 
                     pro_date_modify =:date_modify 
                 WHERE pro_id =:id"; 
         $value = array('name' => $pro_name,
                         'intro' => $pro_intro,
                         'summary' => $pro_summary,
                         'overview' => $pro_overview,
                         'story' => $pro_story,
                         'science' => $pro_science,
                         'benefit' => $pro_benefit,
                         'image' => $pro_image,
                         'image_video' => $pro_image_video,
                         'video' => $pro_video,
                         'date_modify' => $pro_date,
                         'id' => $id
                         );
         $db = new DB();
         //echoArray($value);die();
         $exe = $db->query($sql,$value);
         if($exe) $alert = Str::alert("Cập nhật thành công");
         
         //Re select
         $sql = "SELECT * FROM products WHERE pro_id =:id";
         $row = $db->row($sql,array('id' => $id));
      }
      else{
         Form::setError("myForm","Điền đầy đủ thông tin cho sản phẩm");
      }
   }
?>
<!DOCTYPE HTML>
<head>
	<meta http-equiv="content-type" content="text/html" />
	<meta name="author" content="Ta Van Chinh" />
   <?php echo $css_module ?>
   <?php echo $js_module ?>
	<title>Thêm mới sản phẩm</title>
</head>

   <body>
      <div class="head">
         <div class="isw-grid"></div>
         <h1>Thêm mới sản phẩm</h1>      
         <div class="reload_tab" title="Reload"></div>                       
         <div class="clear"></div>
      </div>
   <div class="block-fluid">
   <?php echo isset($alert) ? $alert : ""?>
   <?php 
      $form = new Form("myForm");
      $form->addElement(new Element_Hidden('action','add'));
      $form->addElement(new Element_Hidden('pro_id',$id));
      $form->addElement(new Element_Hidden('pro_image',$row['pro_image']));
      $form->configure(array(
      	"prevent" => array("bootstrap", "jQuery")
      ));
      $form->addElement(new Element_HTML('<div class="requie">Những dòng đánh dấu * là bắt buộc nhập</div>'));
      $form->addElement(new Element_Textbox('Tên sản phẩm:','pro_name',array(
                        'id' => 'pro_name',
                        'value' => $row['pro_name'],
                        'placeholder' => 'Tên sản phẩm')
                        ));        
      $form->addElement(new Element_File('Ảnh 1(<2MB):','image_field',array(
                        'id' => 'image_field',
                        'accept' =>"image/*"
                        )));
      $form->addElement(new Element_Textbox('Link ảnh video:','pro_image_video',array(
                        'id' => 'pro_image_video',
                        'value' => $row['pro_image_video'],
                        'class' => 'span6',
                        'placeholder' => 'Link ảnh video')
                        ));  
      $form->addElement(new Element_Textbox('Link video:','pro_video',array(
                        'id' => 'pro_video',
                        'value' => $row['pro_video'],
                        'class' => 'span6',
                        'placeholder' => 'Video từ Youtube')
                        ));  
      $form->addElement(new Element_Textarea('Giới thiệu:','pro_intro',array(
                        'id' => 'pro_intro',
                        'value' => $row['pro_intro'],
                        'placeholder' => 'Phần hiển thị đầu trang')
                        ));
      $form->addElement(new Element_Textarea('Tóm tắt:','pro_summary',array(
                        'id' => 'pro_summary',
                        'value' => $row['pro_summary'],
                        'placeholder' => 'Tóm tắt')
                        ));
      $form->addElement(new Element_Textarea('Tổng quan:','pro_overview',array(
                        'id' => 'pro_overview',
                        'value' => $row['pro_overview'],
                        'placeholder' => 'Mô tả tổng quan về sản phẩm')
                        ));
      $form->addElement(new Element_Textarea('Câu chuyện:','pro_story',array(
                        'id' => 'pro_story',
                        'value' => $row['pro_story'],
                        'placeholder' => 'Câu chuyện về sản phẩm')
                        ));
      $form->addElement(new Element_Textarea('Thành phần khoa học:','pro_science',array(
                        'id' => 'pro_science',
                        'value' => $row['pro_science'],
                        'placeholder' => 'Thành phần khoa học')
                        ));
      $form->addElement(new Element_Textarea('Lợi ích:','pro_benefit',array(
                        'id' => 'pro_benefit',
                        'value' => $row['pro_benefit'],
                        'placeholder' => 'Lợi ích đem lại từ sản phẩm')
                        ));    
      $form->addElement(new Element_Button("Cập nhật","submit", array(
         'name' => 'submit',
         'id' => 'submit'
      )));
      $form->addElement(new Element_Button("Hủy", "button", array(
          "onclick" => "history.go(-1);"
      )));
      $form->render();
   ?>
</div>
<script>
   $(document).ready(function(){
      CKEDITOR.replace( 'pro_intro',{
          enterMode:CKEDITOR.ENTER_BR,
      }); 
      CKEDITOR.replace( 'pro_summary',{
          enterMode:CKEDITOR.ENTER_BR,
      }); 
      CKEDITOR.replace( 'pro_overview',{
          enterMode:CKEDITOR.ENTER_BR,
      }); 
      CKEDITOR.replace( 'pro_story',{
          enterMode:CKEDITOR.ENTER_BR,
      });
      CKEDITOR.replace( 'pro_science',{
          enterMode:CKEDITOR.ENTER_BR,
      });
      CKEDITOR.replace( 'pro_benefit',{
          enterMode:CKEDITOR.ENTER_BR,
      });  
   });
   
</script>
</body>
</html>
